package com.jake.xpage.page;

import android.content.Intent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import java.util.LinkedList;

public class XPageContainer extends FrameLayout {
    @NonNull
    private final XPageManger mPageManager;
    @NonNull
    private final XPage mRootPage;
    private IPageAnimator mDefaultAnimator = new XSlidePageAnimator();
    private IHost mHost;
    private IXPageConfigHandler mPageConfigHandler;

    public XPageContainer(@NonNull XPage rootPage) {
        super(rootPage.getContext());
        mRootPage = rootPage;
        mPageManager = createPageManger();
        mPageManager.addOnPagesChangedListener(mPagesChangedListener);
        mRootPage.setCallback(mPageCallback);
        mPageManager.push(rootPage, null);
    }

    @NonNull
    private XPageManger createPageManger() {
        XPageManagerImpl pageManager = new XPageManagerImpl(this, mPageCallback);
        pageManager.setPageConfigHandler(new IXPageConfigHandler() {
            @Override
            public void configPage(@NonNull XPage page, @NonNull XPageConfig config) {
                if (mPageConfigHandler != null) {
                    mPageConfigHandler.configPage(page, config);
                }
            }
        });
        return pageManager;
    }

    public void setHost(IHost host) {
        this.mHost = host;
    }

    /**
     * 适配状态栏和导航栏样式
     *
     * @param configHandler
     */
    public void setPageConfigHandler(IXPageConfigHandler configHandler) {
        this.mPageConfigHandler = configHandler;
    }

    public void setDefaultPageAnimator(IPageAnimator animator) {
        this.mDefaultAnimator = animator;
    }

    public IPageAnimator getDefaultAnimator() {
        return mDefaultAnimator;
    }

    public void startPage(@NonNull XPage page, IPageAnimator animator) {
        getPageManager().push(page, animator);
    }

    public boolean onBackPressed() {
        XPage page = getPageManager().peek();
        if (page != null) {
            return page.onBackPressed();
        }
        return false;
    }

    private void configPageInfo(@NonNull XPage page) {
        XPageConfig info = page.pageInfo;
        if (info.statusBarShow) {
            //设置全屏
        } else if (info.immersionStatusBar) {
            if (info.statusBarDark) {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                    // 状态栏文字颜色设置为黑色
//                    View decorView = getWindow().getDecorView();
//                    int systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
//                    decorView.setSystemUiVisibility(systemUiVisibility);
//                }
                //设置黑色
            } else {
                //设置白色
            }
        }
    }

    private final XPageManger.OnPagesChangedListener mPagesChangedListener = new XPageManger.OnPagesChangedListener() {
        @Override
        public void onChanged(int event, @Nullable XPage currentPage, @Nullable XPage targetPage) {
            if (event == EVENT_AFTER_PUSH || event == EVENT_AFTER_POP) {
                if (targetPage != null) {
                    configPageInfo(targetPage);
                }
            } else if (event == EVENT_AFTER_REMOVE) {
                if (currentPage == targetPage) {
                    XPage page = getPageManager().peek();
                    if (page != null) {
                        configPageInfo(page);
                    }
                }
            }
        }

    };

    private final XPage.Callback mPageCallback = new XPage.Callback() {
        @Override
        public void finish(@NonNull XPage page, @Nullable IPageAnimator animator) {
            finishPage(page, animator);
        }

        @Override
        public void finish(@NonNull XPage page, boolean animate) {
            finishPage(page, animate ? mDefaultAnimator : null);
        }

        private void finishPage(@NonNull XPage page, @Nullable IPageAnimator animator) {
            if (page == mRootPage) {
                if (mHost != null) {
                    mHost.closeContainer();
                }
                return;
            }
            XPage topPage = getPageManager().peek();
            if (topPage == page) {
                getPageManager().pop(animator);
            } else {
                getPageManager().remove(page);
            }
        }

        @Override
        public void startPage(@NonNull XPage page, @Nullable IPageAnimator animator) {
            XPageContainer.this.startPage(page, animator);
        }

        @Override
        public void startPage(@NonNull XPage page, boolean animate) {
            XPageContainer.this.startPage(page, animate ? mDefaultAnimator : null);
        }
    };

    public LifecycleEventObserver getLifecycleObserver() {
        return mLifecycleObserver;
    }

    private final LifecycleEventObserver mLifecycleObserver = new LifecycleEventObserver() {

        @Override
        public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
            XPage currentPage = mPageManager.peek();
            if (currentPage != null) {
                currentPage.getLifecycleEventObserver().onStateChanged(lifecycleOwner, event);
            }
        }
    };

    @NonNull
    public XPageManger getPageManager() {
        return mPageManager;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        LinkedList<XPage> list = getPageManager().getAllPages();
        for (XPage page : list) {
            if (page != null) {
                page.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        LinkedList<XPage> list = getPageManager().getAllPages();
        for (XPage page : list) {
            if (page != null) {
                page.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

    public interface IHost {
        void closeContainer();
    }

}
